<?php

class Application_Form_ReservierungAnlegenForm extends Zend_Form
{
	protected $_parkplatznr;
	protected $_ticketnr;
	protected $_datumswahl;
	
	public function __construct($options = null, $parkplatznr = '', $ticketnr = '', $datumswahl = false)
    {
        $this->_parkplatznr = $parkplatznr;
        $this->_ticketnr = $ticketnr;
        $this->_datumswahl = $datumswahl;
        parent::__construct($options);
    }
    
    public function init()
    {
        /* Form Elements & Other Definitions Here ... */
    	    $decorator = new My_Decorator_SimpleInput();
    	    $decorator2 = new My_Decorator_ReadonlyInput();
    	    $decoratorSubmit = new My_Decorator_Submit();
    	    
    	$this->setName('Reservierung erstellen');

    	//Elemente
		$parkbereich   = new Zend_Form_Element('parkplatznr', array(
        'label'      => 'Parkbereich',
        'value'      => $this->_parkplatznr,
        
    	));
    	
    	$parkbereich
    	->setRequired()
    	->clearDecorators()
    	->addDecorator($decorator2)
    	->addDecorator('Errors');
    	
		
    	$ticketart   = new Zend_Form_Element('ticketnr', array(
        'label'      => 'Ticketart',
        'value'      => $this->_ticketnr,
        ));
    	$ticketart->setRequired()
    	->addErrorMessage('Es muss erst ein Ticket ausgewählt werden.');
    	$ticketart
    	->clearDecorators()
    	->addDecorator($decorator2)
    	->addDecorator('Errors');
			  
    	
		$datumBeginn   = new Zend_Form_Element('datumvon', array(
        'label'      => 'Beginn',
        'value'      => '',
		'validators' => array(
                array('validator' => 'StringLength', 'options' => array(10, 10))
                )
    	));
    	$datumBeginn->setRequired();
    	$datumBeginn->addErrorMessage('Ungültiges Datum');
    	$datumBeginn
    	->clearDecorators()
    	->addDecorator($decorator)
    	->addDecorator('Errors');


    	
		$submit = new Zend_Form_Element_Submit('reservieren',array('class' => 'button'));
		$submit->setValue('Jetzt reservieren')
		->clearDecorators()
    	->addDecorator($decoratorSubmit);
			   
		$form = new Zend_Form();
		
    	//abfrage, ob ein beginndatum angegeben werden kann/darf
    	if ($this->_datumswahl == true){
    		$this->addElements(array($parkbereich,$ticketart,$datumBeginn,$submit));
    	}else{
    		$this->addElements(array($parkbereich,$ticketart,$submit));
    	}
		
		$this->setMethod('post');
    }
		public function loadDefaultDecorators() {
			 $this->setDecorators(
         array(
             array('ViewScript', 
                 array('viewScript' => 'reservierung/datumsauswahl.phtml')
             )
          )
      );     
		}
    


}

class My_Decorator_SimpleInput extends Zend_Form_Decorator_Abstract
    {
        protected $_format = '<label for="%s">%s</label><input id="%s" name="%s" type="text" value="%s"/>';
     
        public function render($content)
        {
            $element = $this->getElement();
            $name    = htmlentities($element->getFullyQualifiedName());
            $label   = htmlentities($element->getLabel());
            $id      = htmlentities($element->getId());
            $value   = htmlentities($element->getValue());
     
            $markup  = sprintf($this->_format, $name, $label, $id, $name, $value);
            return $markup;
        }
    }

class My_Decorator_ReadonlyInput extends Zend_Form_Decorator_Abstract
    {
        protected $_format = '<label for="%s">%s</label><input id="%s" name="%s" type="text" value="%s" readonly="readonly"/>';
     
        public function render($content)
        {
            $element = $this->getElement();
            $name    = htmlentities($element->getFullyQualifiedName());
            $label   = htmlentities($element->getLabel());
            $id      = htmlentities($element->getId());
            $value   = htmlentities($element->getValue());
     
            $markup  = sprintf($this->_format, $name, $label, $id, $name, $value);
            return $markup;
        }
    }
    
class My_Decorator_Submit extends Zend_Form_Decorator_Abstract
    {
        protected $_format = '<input id="%s" name="%s" type="submit" value="%s" class="button"/>';
     
        public function render($content)
        {
            $element = $this->getElement();
            $name    = htmlentities($element->getFullyQualifiedName());
            $label   = htmlentities($element->getLabel());
            $id      = htmlentities($element->getId());
            $value   = htmlentities($element->getValue());
     
            $markup  = sprintf($this->_format, $id, $name, $value);
            return $markup;
        }
    }